using System.Text;
using BizTalk.BuildGenerator.MetaData;

namespace BizTalk.BuildGenerator.BuildFrameworks.Default.FileBuilders
{
    /// <summary>
    /// Builds the App.Custom.targets file
    /// </summary>
    public class CustomPropertiesFileBuilder : IFileBuilder
    {
        private const string FileExtension = ".Custom.properties";

        #region IFileBuilder Members

        public void Build(GenerationArgs args)
        {
            var fileBuilder = new StringBuilder();
            fileBuilder.Append(Properties.Resources.App_Custom_Properties);
            GenerationTags.ReplaceTags(fileBuilder, args);

            if (args.BuildProperties != null && args.BuildProperties.Properties != null)
            {
                var sb = new StringBuilder();
                foreach (var property in args.BuildProperties.Properties)
                {
                    sb.AppendFormat("<{0}>{1}</{0}>", property.PropertyName, property.PropertyValue);
                    sb.AppendLine();
                }
                fileBuilder.Replace("<!-- @CustomProperties@ -->", sb.ToString());
            }
            var filePath = args.OutputFolder + @"\" + args.ApplicationDescription.Name + FileExtension;
            FileHelper.WriteFile(filePath, fileBuilder);
        }

        #endregion
    }
}